<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{&quot;hostname&quot;:&quot;www.nilstorm.com&quot;,&quot;root&quot;:&quot;&#x2F;&quot;,&quot;images&quot;:&quot;&#x2F;images&quot;,&quot;scheme&quot;:&quot;Gemini&quot;,&quot;version&quot;:&quot;8.5.0&quot;,&quot;exturl&quot;:false,&quot;sidebar&quot;:{&quot;position&quot;:&quot;left&quot;,&quot;display&quot;:&quot;post&quot;,&quot;padding&quot;:18,&quot;offset&quot;:12},&quot;copycode&quot;:false,&quot;bookmark&quot;:{&quot;enable&quot;:false,&quot;color&quot;:&quot;#222&quot;,&quot;save&quot;:&quot;auto&quot;},&quot;fancybox&quot;:false,&quot;mediumzoom&quot;:false,&quot;lazyload&quot;:false,&quot;pangu&quot;:false,&quot;comments&quot;:{&quot;style&quot;:&quot;tabs&quot;,&quot;active&quot;:null,&quot;storage&quot;:true,&quot;lazyload&quot;:false,&quot;nav&quot;:null},&quot;motion&quot;:{&quot;enable&quot;:true,&quot;async&quot;:false,&quot;transition&quot;:{&quot;post_block&quot;:&quot;fadeIn&quot;,&quot;post_header&quot;:&quot;fadeInDown&quot;,&quot;post_body&quot;:&quot;fadeInDown&quot;,&quot;coll_header&quot;:&quot;fadeInLeft&quot;,&quot;sidebar&quot;:&quot;fadeInUp&quot;}},&quot;prism&quot;:false,&quot;i18n&quot;:{&quot;placeholder&quot;:&quot;Searching...&quot;,&quot;empty&quot;:&quot;We didn&#39;t find any results for the search: ${query}&quot;,&quot;hits_time&quot;:&quot;${hits} results found in ${time} ms&quot;,&quot;hits&quot;:&quot;${hits} results found&quot;}}</script><script src="/js/config.js"></script>
<meta name="description" content="DL_SYNCHRONIZATION_ON_BOOLEANSynchronization on Boolean could lead to deadlock该代码同步一个封装的原始常量，例如一个Boolean类型private static Boolean inited &#x3D; Boolean.FALSE; ...   synchronized(inited) &amp;#123;      if (!ini">
<meta property="og:type" content="article">
<meta property="og:title" content="FindBugs 规则整理：Multithreaded Correctness">
<meta property="og:url" content="http://www.nilstorm.com/2017/baebe2db.html">
<meta property="og:site_name" content="Lam&#39;s Blog">
<meta property="og:description" content="DL_SYNCHRONIZATION_ON_BOOLEANSynchronization on Boolean could lead to deadlock该代码同步一个封装的原始常量，例如一个Boolean类型private static Boolean inited &#x3D; Boolean.FALSE; ...   synchronized(inited) &amp;#123;      if (!ini">
<meta property="og:locale">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">
<meta property="article:published_time" content="2017-03-03T02:36:56.000Z">
<meta property="article:modified_time" content="2021-06-06T08:08:18.170Z">
<meta property="article:author" content="LinBinghe">
<meta property="article:tag" content="FindBugs">
<meta property="article:tag" content="规则">
<meta property="article:tag" content="Multithreaded Correctness">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">


<link rel="canonical" href="http://www.nilstorm.com/2017/baebe2db.html">



<script class="next-config" data-name="page" type="application/json">{&quot;sidebar&quot;:&quot;&quot;,&quot;isHome&quot;:false,&quot;isPost&quot;:true,&quot;lang&quot;:&quot;zh-Hans&quot;,&quot;comments&quot;:true,&quot;permalink&quot;:&quot;http:&#x2F;&#x2F;www.nilstorm.com&#x2F;2017&#x2F;baebe2db.html&quot;,&quot;path&quot;:&quot;2017&#x2F;baebe2db.html&quot;,&quot;title&quot;:&quot;FindBugs 规则整理：Multithreaded Correctness&quot;}</script>

<script class="next-config" data-name="calendar" type="application/json">&quot;&quot;</script>
<title>FindBugs 规则整理：Multithreaded Correctness | Lam's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Lam's Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lam's Blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Knowledge as Action</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#DL-SYNCHRONIZATION-ON-BOOLEAN"><span class="nav-number">1.</span> <span class="nav-text">DL_SYNCHRONIZATION_ON_BOOLEAN</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DL-SYNCHRONIZATION-ON-BOXED-PRIMITIVE"><span class="nav-number">2.</span> <span class="nav-text">DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DL-SYNCHRONIZATION-ON-SHARED-CONSTANT"><span class="nav-number">3.</span> <span class="nav-text">DL_SYNCHRONIZATION_ON_SHARED_CONSTANT</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DL-SYNCHRONIZATION-ON-UNSHARED-BOXED-PRIMITIVE"><span class="nav-number">4.</span> <span class="nav-text">DL_SYNCHRONIZATION_ON_UNSHARED_BOXED_PRIMITIVE</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-MONITOR-WAIT-ON-CONDITION"><span class="nav-number">5.</span> <span class="nav-text">DM_MONITOR_WAIT_ON_CONDITION</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#DM-USELESS-THREAD"><span class="nav-number">6.</span> <span class="nav-text">DM_USELESS_THREAD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ESync-EMPTY-SYNC"><span class="nav-number">7.</span> <span class="nav-text">ESync_EMPTY_SYNC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#IS2-INCONSISTENT-SYNC"><span class="nav-number">8.</span> <span class="nav-text">IS2_INCONSISTENT_SYNC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#IS-FIELD-NOT-GUARDED"><span class="nav-number">9.</span> <span class="nav-text">IS_FIELD_NOT_GUARDED</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#JLM-JSR166-LOCK-MONITORENTER"><span class="nav-number">10.</span> <span class="nav-text">JLM_JSR166_LOCK_MONITORENTER</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#LI-LAZY-INIT-STATIC"><span class="nav-number">11.</span> <span class="nav-text">LI_LAZY_INIT_STATIC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#LI-LAZY-INIT-UPDATE-STATIC"><span class="nav-number">12.</span> <span class="nav-text">LI_LAZY_INIT_UPDATE_STATIC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#ML-SYNC-ON-UPDATED-FIELD"><span class="nav-number">13.</span> <span class="nav-text">ML_SYNC_ON_UPDATED_FIELD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#MSF-MUTABLE-SERVLET-FIELD"><span class="nav-number">14.</span> <span class="nav-text">MSF_MUTABLE_SERVLET_FIELD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#MWN-MISMATCHED-NOTIFY"><span class="nav-number">15.</span> <span class="nav-text">MWN_MISMATCHED_NOTIFY</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#MWN-MISMATCHED-WAIT"><span class="nav-number">16.</span> <span class="nav-text">MWN_MISMATCHED_WAIT</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#NP-SYNC-AND-NULL-CHECK-FIELD"><span class="nav-number">17.</span> <span class="nav-text">NP_SYNC_AND_NULL_CHECK_FIELD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#NO-NOTIFY-NOT-NOTIFYALL"><span class="nav-number">18.</span> <span class="nav-text">NO_NOTIFY_NOT_NOTIFYALL</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#RS-READOBJECT-SYNC"><span class="nav-number">19.</span> <span class="nav-text">RS_READOBJECT_SYNC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#RU-INVOKE-RUN"><span class="nav-number">20.</span> <span class="nav-text">RU_INVOKE_RUN</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SC-START-IN-CTOR"><span class="nav-number">21.</span> <span class="nav-text">SC_START_IN_CTOR</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SP-SPIN-ON-FIELD"><span class="nav-number">22.</span> <span class="nav-text">SP_SPIN_ON_FIELD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#STCAL-INVOKE-ON-STATIC-CALENDAR-INSTANCE"><span class="nav-number">23.</span> <span class="nav-text">STCAL_INVOKE_ON_STATIC_CALENDAR_INSTANCE</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#STCAL-INVOKE-ON-STATIC-DATE-FORMAT-INSTANCE"><span class="nav-number">24.</span> <span class="nav-text">STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#STCAL-STATIC-CALENDAR-INSTANCE"><span class="nav-number">25.</span> <span class="nav-text">STCAL_STATIC_CALENDAR_INSTANCE</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#STCAL-STATIC-SIMPLE-DATE-FORMAT-INSTANCE"><span class="nav-number">26.</span> <span class="nav-text">STCAL_STATIC_SIMPLE_DATE_FORMAT_INSTANCE</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SWL-SLEEP-WITH-LOCK-HELD"><span class="nav-number">27.</span> <span class="nav-text">SWL_SLEEP_WITH_LOCK_HELD</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UG-SYNC-SET-UNSYNC-GET"><span class="nav-number">28.</span> <span class="nav-text">UG_SYNC_SET_UNSYNC_GET</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UL-UNRELEASED-LOCK"><span class="nav-number">29.</span> <span class="nav-text">UL_UNRELEASED_LOCK</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UL-UNRELEASED-LOCK-EXCEPTION-PATH"><span class="nav-number">30.</span> <span class="nav-text">UL_UNRELEASED_LOCK_EXCEPTION_PATH</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UW-UNCOND-WAIT"><span class="nav-number">31.</span> <span class="nav-text">UW_UNCOND_WAIT</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#VO-VOLATILE-REFERENCE-TO-ARRAY"><span class="nav-number">32.</span> <span class="nav-text">VO_VOLATILE_REFERENCE_TO_ARRAY</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#WL-USING-GETCLASS-RATHER-THAN-CLASS-LITERAL"><span class="nav-number">33.</span> <span class="nav-text">WL_USING_GETCLASS_RATHER_THAN_CLASS_LITERAL</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#WS-WRITEOBJECT-SYNC"><span class="nav-number">34.</span> <span class="nav-text">WS_WRITEOBJECT_SYNC</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#WA-AWAIT-NOT-IN-LOOP"><span class="nav-number">35.</span> <span class="nav-text">WA_AWAIT_NOT_IN_LOOP</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#WA-NOT-IN-LOOP"><span class="nav-number">36.</span> <span class="nav-text">WA_NOT_IN_LOOP</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%B6%E4%BB%96%E6%96%87%E7%AB%A0%EF%BC%88%E6%8C%81%E7%BB%AD%E6%9B%B4%E6%96%B0%EF%BC%89"><span class="nav-number">37.</span> <span class="nav-text">其他文章（持续更新）</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BC%95%E7%94%A8"><span class="nav-number">38.</span> <span class="nav-text">引用</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LinBinghe"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">LinBinghe</p>
  <div class="site-description" itemprop="description">Knowledge as Action</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">60</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LinBinghe" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LinBinghe" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/shawn_lam" title="Zhihu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;shawn_lam" rel="noopener" target="_blank"><i class="fab fa-zhihu fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:linbinghe@gmail.com" title="E-Mail → mailto:linbinghe@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://www.nilstorm.com/2017/baebe2db.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="LinBinghe">
      <meta itemprop="description" content="Knowledge as Action">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lam's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          FindBugs 规则整理：Multithreaded Correctness
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2017-03-03 10:36:56" itemprop="dateCreated datePublished" datetime="2017-03-03T10:36:56+08:00">2017-03-03</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-06-06 16:08:18" itemprop="dateModified" datetime="2021-06-06T16:08:18+08:00">2021-06-06</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/FindBugs/" itemprop="url" rel="index"><span itemprop="name">FindBugs</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="DL-SYNCHRONIZATION-ON-BOOLEAN"><a href="#DL-SYNCHRONIZATION-ON-BOOLEAN" class="headerlink" title="DL_SYNCHRONIZATION_ON_BOOLEAN"></a>DL_SYNCHRONIZATION_ON_BOOLEAN</h1><p><em>Synchronization on Boolean could lead to deadlock</em><br>该代码同步一个封装的原始常量，例如一个Boolean类型<br><code>private static Boolean inited = Boolean.FALSE;
...
  synchronized(inited) &#123; 
    if (!inited) &#123;
       init();
       inited = Boolean.TRUE;
       &#125;
     &#125;
...</code><br><span id="more"></span><br>由于通常只存在两个布尔对象，此代码可能是同步的其他无关的代码中相同的对象，这时会导致反应迟钝和可能死锁</p>
<h1 id="DL-SYNCHRONIZATION-ON-BOXED-PRIMITIVE"><a href="#DL-SYNCHRONIZATION-ON-BOXED-PRIMITIVE" class="headerlink" title="DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE"></a>DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE</h1><p><em>Synchronization on boxed primitive could lead to deadlock</em><br>该代码同步一个封装的原始常量，例如一个Integer类型。<br><code>private static Integer count = 0;
...
  synchronized(count) &#123; 
     count++;
     &#125;
...</code><br>由于Integer对象可以共享和保存，此代码可能是同步的其他无关的代码中相同的对象，这时会导致反应迟钝和可能死锁</p>
<h1 id="DL-SYNCHRONIZATION-ON-SHARED-CONSTANT"><a href="#DL-SYNCHRONIZATION-ON-SHARED-CONSTANT" class="headerlink" title="DL_SYNCHRONIZATION_ON_SHARED_CONSTANT"></a>DL_SYNCHRONIZATION_ON_SHARED_CONSTANT</h1><p><em>Synchronization on interned String could lead to deadlock</em><br>同步String类型的常量时，由于它被JVM中多个其他的对象所共有，这样在其他代码中会引起死锁。</p>
<h1 id="DL-SYNCHRONIZATION-ON-UNSHARED-BOXED-PRIMITIVE"><a href="#DL-SYNCHRONIZATION-ON-UNSHARED-BOXED-PRIMITIVE" class="headerlink" title="DL_SYNCHRONIZATION_ON_UNSHARED_BOXED_PRIMITIVE"></a>DL_SYNCHRONIZATION_ON_UNSHARED_BOXED_PRIMITIVE</h1><p><em>Synchronization on boxed primitive values</em><br>同步一个显然不是共有封装的原始值，例如一个Integer类型的对象。例如：<br><code>private static final Integer fileLock = new Integer(1);
...
  synchronized(fileLock) &#123; 
     .. do something ..
     &#125;
...</code><br>它最后被定义为以下方式来代替：private static final Object fileLock = new Object();</p>
<h1 id="DM-MONITOR-WAIT-ON-CONDITION"><a href="#DM-MONITOR-WAIT-ON-CONDITION" class="headerlink" title="DM_MONITOR_WAIT_ON_CONDITION"></a>DM_MONITOR_WAIT_ON_CONDITION</h1><p><em>Monitor wait() called on Condition</em><br>方法中以java.util.concurrent.locks.Condition对象调用wait（）。等待一个条件发生时应该使用在Condition接口中定义的await()方法。</p>
<h1 id="DM-USELESS-THREAD"><a href="#DM-USELESS-THREAD" class="headerlink" title="DM_USELESS_THREAD"></a>DM_USELESS_THREAD</h1><p><em>A thread was created using the default empty run method</em><br>这个方法没有通过run方法或者具体声明Thread类，也没有通过一个Runnable对象去定义一个线程，而这个线程出来浪费资源却什么也没有去做。</p>
<h1 id="ESync-EMPTY-SYNC"><a href="#ESync-EMPTY-SYNC" class="headerlink" title="ESync_EMPTY_SYNC"></a>ESync_EMPTY_SYNC</h1><p><em>Empty synchronized block</em><br>该代码包含一个空的同步块：synchronized() {}</p>
<h1 id="IS2-INCONSISTENT-SYNC"><a href="#IS2-INCONSISTENT-SYNC" class="headerlink" title="IS2_INCONSISTENT_SYNC"></a>IS2_INCONSISTENT_SYNC</h1><p><em>Inconsistent synchronization</em><br>不合理的同步</p>
<h1 id="IS-FIELD-NOT-GUARDED"><a href="#IS-FIELD-NOT-GUARDED" class="headerlink" title="IS_FIELD_NOT_GUARDED"></a>IS_FIELD_NOT_GUARDED</h1><p><em>Field not guarded against concurrent access</em><br> 属性不能保证同步访问</p>
<h1 id="JLM-JSR166-LOCK-MONITORENTER"><a href="#JLM-JSR166-LOCK-MONITORENTER" class="headerlink" title="JLM_JSR166_LOCK_MONITORENTER"></a>JLM_JSR166_LOCK_MONITORENTER</h1><p><em>Synchronization performed on Lock</em><br>实现java.util.concurrent.locks.Lock的对象调用了同步的方法。应该这样处理，对象被锁定/解锁时使用acquire（）/ release（）方法而不是使用同步的方法。</p>
<h1 id="LI-LAZY-INIT-STATIC"><a href="#LI-LAZY-INIT-STATIC" class="headerlink" title="LI_LAZY_INIT_STATIC"></a>LI_LAZY_INIT_STATIC</h1><p><em>Incorrect lazy initialization of static field</em><br>错误的对static属性进行了延迟初始化</p>
<h1 id="LI-LAZY-INIT-UPDATE-STATIC"><a href="#LI-LAZY-INIT-UPDATE-STATIC" class="headerlink" title="LI_LAZY_INIT_UPDATE_STATIC"></a>LI_LAZY_INIT_UPDATE_STATIC</h1><p><em>Incorrect lazy initialization and update of static field</em><br>这种方法包含一个不同步延迟初始化的静态字段。之后为字段赋值，对象存储到该位置后进一步更新或访问。字段后尽快让其他线程能够访问。如果该方法的进一步访问该字段为初始化对象提供服务，然后你有一个非常严重的多线程bug，除非别的东西阻止任何其他线程访问存储的对象，直到它完全初始化。<br>即使你有信心，该方法是永远不会被多个线程调用时，在它的值还没有被充分初始化或移动，不把它设定为static字段时它可能会更好。</p>
<h1 id="ML-SYNC-ON-UPDATED-FIELD"><a href="#ML-SYNC-ON-UPDATED-FIELD" class="headerlink" title="ML_SYNC_ON_UPDATED_FIELD"></a>ML_SYNC_ON_UPDATED_FIELD</h1><p><em>Method synchronizes on an updated field</em><br>对象获取一个可变字段时进行同步。这是没有意义的，因为不同的线程可以在不同的对象同步。</p>
<h1 id="MSF-MUTABLE-SERVLET-FIELD"><a href="#MSF-MUTABLE-SERVLET-FIELD" class="headerlink" title="MSF_MUTABLE_SERVLET_FIELD"></a>MSF_MUTABLE_SERVLET_FIELD</h1><p><em>Mutable servlet field</em><br>一个web服务一般只能创建一个servlet或者jsp的实例（例如：treates是一个单利类），它会被多个线程调用这个实例的方法服务于多个同时的请求。因此使用易变的字段属性产生竞争的情况。</p>
<h1 id="MWN-MISMATCHED-NOTIFY"><a href="#MWN-MISMATCHED-NOTIFY" class="headerlink" title="MWN_MISMATCHED_NOTIFY"></a>MWN_MISMATCHED_NOTIFY</h1><p><em>Mismatched notify()</em><br>此方法调用Object.notify（）或Object.notifyAll（）而没有获取到该对象的对象锁。调用notify（）或notifyAll（）而没有持有该对象的对象锁，将导致IllegalMonitorStateException异常。</p>
<h1 id="MWN-MISMATCHED-WAIT"><a href="#MWN-MISMATCHED-WAIT" class="headerlink" title="MWN_MISMATCHED_WAIT"></a>MWN_MISMATCHED_WAIT</h1><p><em>Mismatched wait()</em><br>此方法调用Object.wait()而没有获取到该对象的对象锁。调用wait（）而没有持有该对象的对象锁，将导致IllegalMonitorStateException异常。</p>
<h1 id="NP-SYNC-AND-NULL-CHECK-FIELD"><a href="#NP-SYNC-AND-NULL-CHECK-FIELD" class="headerlink" title="NP_SYNC_AND_NULL_CHECK_FIELD"></a>NP_SYNC_AND_NULL_CHECK_FIELD</h1><p><em>Synchronize and null check on the same field.</em><br>如果代码块是同步的，那么久不可能为空。如果是空，同步时就会抛出NullPointerException异常。最好是在另一个代码块中进行同步。</p>
<h1 id="NO-NOTIFY-NOT-NOTIFYALL"><a href="#NO-NOTIFY-NOT-NOTIFYALL" class="headerlink" title="NO_NOTIFY_NOT_NOTIFYALL"></a>NO_NOTIFY_NOT_NOTIFYALL</h1><p><em>Using notify() rather than notifyAll()</em><br>调用 notify() 而不是 notifyAll() 方法。 Java的监控器通常用于多个条件。调用 notify() 只唤醒一个线程，这意味着该线程被唤醒只是满足的当前的唯一条件。</p>
<h1 id="RS-READOBJECT-SYNC"><a href="#RS-READOBJECT-SYNC" class="headerlink" title="RS_READOBJECT_SYNC"></a>RS_READOBJECT_SYNC</h1><p><em>Class’s readObject() method is synchronized</em><br>序列化类中定义了同步的readObject（）。通过定义，反序列化创建的对象只有一个线程可以访问，因此没有必要的readObject（）进行同步。如果的readObject（）方法本身造成对象对另一个线程可见，那么这本身就是不好的编码方式。</p>
<h1 id="RU-INVOKE-RUN"><a href="#RU-INVOKE-RUN" class="headerlink" title="RU_INVOKE_RUN"></a>RU_INVOKE_RUN</h1><p><em>Invokes run on a thread (did you mean to start it instead?)</em><br>这种方法显式调用一个对象的 run()。一般来说，类是实现Runnable接口的，因为在一个新的线程他们将有自己的 run() 方法，在这种情况下 Thread.start() 方法调用是正确的。</p>
<h1 id="SC-START-IN-CTOR"><a href="#SC-START-IN-CTOR" class="headerlink" title="SC_START_IN_CTOR"></a>SC_START_IN_CTOR</h1><p><em>Constructor invokes Thread.start()</em><br>在构造函数中启动一个线程。如果类曾经被子类扩展过，那么这很可能是错的，因为线程将在子类构造之前开始启动。</p>
<h1 id="SP-SPIN-ON-FIELD"><a href="#SP-SPIN-ON-FIELD" class="headerlink" title="SP_SPIN_ON_FIELD"></a>SP_SPIN_ON_FIELD</h1><p><em>Method spins on field</em><br>方法无限循环读取一个字段。编译器可合法悬挂宣读循环，变成一个无限循环的代码。这个类应该改变，所以使用适当的同步（包括等待和通知要求）</p>
<h1 id="STCAL-INVOKE-ON-STATIC-CALENDAR-INSTANCE"><a href="#STCAL-INVOKE-ON-STATIC-CALENDAR-INSTANCE" class="headerlink" title="STCAL_INVOKE_ON_STATIC_CALENDAR_INSTANCE"></a>STCAL_INVOKE_ON_STATIC_CALENDAR_INSTANCE</h1><p><em>Call to static Calendar</em><br>即使JavaDoc对此不包含暗示，而Calendars本身在多线程中使用就是不安全的。探测器发现当调用Calendars的实例时将会获得一个静态对象。<br>Calendar rightNow = Calendar.getInstance();</p>
<h1 id="STCAL-INVOKE-ON-STATIC-DATE-FORMAT-INSTANCE"><a href="#STCAL-INVOKE-ON-STATIC-DATE-FORMAT-INSTANCE" class="headerlink" title="STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE"></a>STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE</h1><p><em>Call to static DateFormat</em><br>在官方的JavaDoc，DateFormats多线程使用本事就是不安全的。探测器发现调用一个DateFormat的实例将会获得一个静态对象。<br>myString = DateFormat.getDateInstance().format(myDate);</p>
<h1 id="STCAL-STATIC-CALENDAR-INSTANCE"><a href="#STCAL-STATIC-CALENDAR-INSTANCE" class="headerlink" title="STCAL_STATIC_CALENDAR_INSTANCE"></a>STCAL_STATIC_CALENDAR_INSTANCE</h1><p><em>Static Calendar</em><br>Calendar在多线程中本身就是不安全的，如果在线程范围中共享一个Calendarde 实例而不使用一个同步的方法在应用中就会出现一些奇怪的行为。在sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate()中会抛出ArrayIndexOutOfBoundsExceptions or IndexOutOfBoundsExceptions异常。</p>
<h1 id="STCAL-STATIC-SIMPLE-DATE-FORMAT-INSTANCE"><a href="#STCAL-STATIC-SIMPLE-DATE-FORMAT-INSTANCE" class="headerlink" title="STCAL_STATIC_SIMPLE_DATE_FORMAT_INSTANCE"></a>STCAL_STATIC_SIMPLE_DATE_FORMAT_INSTANCE</h1><p><em>Static DateFormat</em><br>DateFormat 在多线程中本身就是不安全的，如果在线程范围中共享一个DateFormat的实例而不使用一个同步的方法在应用中就会出现一些奇怪的行为。</p>
<h1 id="SWL-SLEEP-WITH-LOCK-HELD"><a href="#SWL-SLEEP-WITH-LOCK-HELD" class="headerlink" title="SWL_SLEEP_WITH_LOCK_HELD"></a>SWL_SLEEP_WITH_LOCK_HELD</h1><p><em>Method calls Thread.sleep() with a lock held</em><br>当持有对象时调用Thread.sleep（）。这可能会导致很差的性能和可扩展性，或陷入死锁，因为其他线程可能正在等待获得锁。调用wait（）是一个更好的主意，释放对象的持有以允许其他线程运行。</p>
<h1 id="UG-SYNC-SET-UNSYNC-GET"><a href="#UG-SYNC-SET-UNSYNC-GET" class="headerlink" title="UG_SYNC_SET_UNSYNC_GET"></a>UG_SYNC_SET_UNSYNC_GET</h1><p><em>Unsynchronized get method, synchronized set method</em><br>这个类包含类似命名的get和set方法。在set方法是同步方法和get方法是非同步方法。这可能会导致在运行时的不正确行为，因为调用的get方法不一定返回对象一致状态。 GET方法应该同步。</p>
<h1 id="UL-UNRELEASED-LOCK"><a href="#UL-UNRELEASED-LOCK" class="headerlink" title="UL_UNRELEASED_LOCK"></a>UL_UNRELEASED_LOCK</h1><p><em>Method does not release lock on all paths</em><br>方法获得了当前的对象所，但是在方法中始终没有释放它。一个正确的示例如下：<br>  <code>Lock l = ...;
   l.lock();
    try &#123;
        // do something
    &#125; finally &#123;
        l.unlock();
    &#125;</code></p>
<h1 id="UL-UNRELEASED-LOCK-EXCEPTION-PATH"><a href="#UL-UNRELEASED-LOCK-EXCEPTION-PATH" class="headerlink" title="UL_UNRELEASED_LOCK_EXCEPTION_PATH"></a>UL_UNRELEASED_LOCK_EXCEPTION_PATH</h1><p>方法获得了当前的对象所，但是在所有的异常处理中始终没有释放它。正确的示例同UL_UNRELEASED_LOCK</p>
<h1 id="UW-UNCOND-WAIT"><a href="#UW-UNCOND-WAIT" class="headerlink" title="UW_UNCOND_WAIT"></a>UW_UNCOND_WAIT</h1><p><em>Unconditional wait</em><br>方法中包含调用java.lang.Object.wait（），而却没有放到条件流程控制中。该代码应确认条件尚未满足之前等待;先前任何通知将被忽略。</p>
<h1 id="VO-VOLATILE-REFERENCE-TO-ARRAY"><a href="#VO-VOLATILE-REFERENCE-TO-ARRAY" class="headerlink" title="VO_VOLATILE_REFERENCE_TO_ARRAY"></a>VO_VOLATILE_REFERENCE_TO_ARRAY</h1><p><em>A volatile reference to an array doesn’t treat the array elements as volatile</em><br>声明一个变量引用数组，这可能不是你想要的。如果一个变量引用数组，那么对引用数组的读和写都是不安全的，但是数组元素不是变量。取得数组的变量值你可以使用java.util.concurrent包中的数组的原子性特性</p>
<h1 id="WL-USING-GETCLASS-RATHER-THAN-CLASS-LITERAL"><a href="#WL-USING-GETCLASS-RATHER-THAN-CLASS-LITERAL" class="headerlink" title="WL_USING_GETCLASS_RATHER_THAN_CLASS_LITERAL"></a>WL_USING_GETCLASS_RATHER_THAN_CLASS_LITERAL</h1><p><em>Sychronization on getClass rather than class literal</em><br>实例的方法中同步this.getClass()，如果这个类有子类集合，那么子类集合中的对象将会在这个类的各个子类上进行同步，这不是我们想要的效果，我们只要同步当前的类对象而不包含它的所有子类，可以同步类名.getClass()。例如，java.awt.Label的代码：<br>     <code>private static final String base = &quot;label&quot;;
     private static int nameCounter = 0;
     String constructComponentName() &#123;
        synchronized (getClass()) &#123;
            return base + nameCounter++;
        &#125;
     &#125;</code><br>Label中的子类集合不可能在同一个子对象上进行同步，替换上面的方法为：<br>    <code>private static final String base = &quot;label&quot;;
     private static int nameCounter = 0;
     String constructComponentName() &#123;
        synchronized (Label.class) &#123;
            return base + nameCounter++;
        &#125;
     &#125;</code></p>
<h1 id="WS-WRITEOBJECT-SYNC"><a href="#WS-WRITEOBJECT-SYNC" class="headerlink" title="WS_WRITEOBJECT_SYNC"></a>WS_WRITEOBJECT_SYNC</h1><p><em>Class’s writeObject() method is synchronized but nothing else is</em><br>这个类有一个writeObject（）方法是同步的，但是这个类中没有其他的同步方法。</p>
<h1 id="WA-AWAIT-NOT-IN-LOOP"><a href="#WA-AWAIT-NOT-IN-LOOP" class="headerlink" title="WA_AWAIT_NOT_IN_LOOP"></a>WA_AWAIT_NOT_IN_LOOP</h1><p><em>Condition.await() not in loop</em><br>方法没有在循环中调用java.util.concurrent.await()。如果对象是用于多种条件，打算调用wait()方法的条件可能不是实际发生的。</p>
<h1 id="WA-NOT-IN-LOOP"><a href="#WA-NOT-IN-LOOP" class="headerlink" title="WA_NOT_IN_LOOP"></a>WA_NOT_IN_LOOP</h1><p><em>Wait not in loop</em><br>这种方法包含调用java.lang.Object.wait()，而这并不是一个循环。如果监视器用于多个条件，打算调用wait()方法的条件可能不是实际发生的。</p>
<h1 id="其他文章（持续更新）"><a href="#其他文章（持续更新）" class="headerlink" title="其他文章（持续更新）"></a>其他文章（持续更新）</h1><p><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/b8a60a5e.html">FindBugs：简介与使用</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/f6c077f9.html">FindBugs 规则整理：Bad Practice</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/eb74f6c5.html">FindBugs 规则整理：Style &amp; Dodgy</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/7a2a5923.html">FindBugs 规则整理：Internationalization</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/7dfffed7.html">FindBugs 规则整理：Malicious Code Vulnerability</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/27613e18.html">FindBugs 规则整理：Security &amp; Experimental</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/8a8d645f.html">FindBugs 规则整理：Performance</a><br><a target="_blank" rel="noopener" href="http://linbinghe.me/2017/b2ff5786.html">FindBugs 规则整理：CORRECTNESS</a></p>
<h1 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h1><p><em>整合以下文章过程中发现部分存在翻译错误，已做修正，同时感谢以下文章作者</em><br><a target="_blank" rel="noopener" href="http://blog.csdn.net/jdsjlzx/article/details/21472253/">FindBugs规则整理</a></p>
<hr>
<p><strong>版权声明</strong></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg" alt="Creative Commons BY-NC-ND 4.0 International License"></p>
<p><a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a> by <a target="_blank" rel="noopener" href="http://linbinghe.me/">Binghe Lin</a> is licensed under a <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons BY-NC-ND 4.0 International License</a>.<br>由<a target="_blank" rel="noopener" href="http://linbinghe.me/">林炳河</a>创作并维护的<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a>采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">创作共用保留署名-非商业-禁止演绎4.0国际许可证</a>。</p>
<p>本文首发于<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog - Knowledeg as Action</a>，版权所有，侵权必究。</p>
<p>本文永久链接：<a target="_blank" rel="noopener" href="http://codinglife.me/2017/baebe2db.html">http://codinglife.me/2017/baebe2db.html</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/FindBugs/" rel="tag"># FindBugs</a>
              <a href="/tags/%E8%A7%84%E5%88%99/" rel="tag"># 规则</a>
              <a href="/tags/Multithreaded-Correctness/" rel="tag"># Multithreaded Correctness</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2017/7dfffed7.html" rel="prev" title="FindBugs 规则整理：Malicious Code Vulnerability">
                  <i class="fa fa-chevron-left"></i> FindBugs 规则整理：Malicious Code Vulnerability
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2017/2.7613e+22.html" rel="next" title="FindBugs 规则整理：Security & Experimental">
                  FindBugs 规则整理：Security & Experimental <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LinBinghe</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  






  





</body>
</html>
